//
//  HMHomeOriginalView.swift
//  Weibo24
//
//  Created by apple on 17/3/30.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
import YYText

//  首页原创微博视图
class HMHomeOriginalView: UIView {
    
    //  MARK: ---懒加载控件
    //  头像
    fileprivate lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级图片
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    fileprivate lazy var screenNameLabel: UILabel = UILabel(fontSize: 14, textColor: UIColor.darkGray, text: "东方不败")
    //  会员等级图片
    fileprivate lazy var mBrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    fileprivate lazy var timeLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.orange, text: "刚刚")
    //  来源
    fileprivate lazy var sourceLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.lightGray, text: "来自: 微博")
    //  微博内容
//    fileprivate lazy var contentLabel: UILabel = {
//        let label = UILabel(fontSize: 13, textColor: UIColor.darkGray, text: "来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你")
//        //  多行显示
//        label.numberOfLines = 0
//        return label
//    }()
    
    fileprivate lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    fileprivate lazy var pictureView: HMHomePictureView = {
        let collectionView = HMHomePictureView()
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    //  记录原创微博底部约束
    var originViewBottomConstraint: Constraint?
    
    //  数据绑定
    var statusViewModel: HMStatusViewModel? {
        didSet {
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                headImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //  昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //  微博内容
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            //  设置认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            //  设置会员等级图片
            mBrankImageView.image = statusViewModel?.mbrankImage
            //  设置来源数据
            sourceLabel.text = statusViewModel?.sourceContent
            //  设置时间
            timeLabel.text = statusViewModel?.timeContent
            
            //  1. 先卸载之前的原创微博的底部约束
            originViewBottomConstraint?.deactivate()
            
            //  判断原创微博是否有配图
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  表示有原创微博的图片
                pictureView.isHidden = false
                //  重新创建约束底边约束
                self.snp.makeConstraints({ (make) in
                   originViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  绑定原创微博配图数据
                pictureView.picUrls = picUrls
                
            } else {
                //  表示没有原创微博的图片
                pictureView.isHidden = true
                self.snp.makeConstraints({ (make) in
                     originViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
         
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    fileprivate func setupUI() {
        //  设置原创微博的颜色
        backgroundColor = UIColor.white
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mBrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.trailing)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp.trailing).offset(HomeCellMargin)
        }
        
        mBrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(HomeCellMargin)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(headImageView.snp.trailing).offset(HomeCellMargin)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  默认值大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //  关键约束
        self.snp.makeConstraints { (make) in
            originViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        //  设置点击高亮文字需要执行的闭包
        contentLabel.highlightTapAction = { (label, labelAttributedStr, range, rect) in
            //  label: 点击视图
            //  labelAttributedStr: 视图显示的富文本
            //  range: 点击高亮文字的范围
            //  rect: 点击整体高亮文字的区域
            let result = (labelAttributedStr.string as NSString).substring(with: range)
            print(result)
        }
    
    }
}
